package wenjun.du.androidart.view

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.ViewConfiguration
import kotlinx.android.synthetic.main.activity_view_base.*
import wenjun.du.androidart.R

/**
 * view相关的基础知识；
 * 都是相对父控件来说
 * 坐标位置相关：left、right、top和bottom
 * 左上角坐标；x、y
 * 控件相对于父控件偏移量：translationX、translationY 默认值都是0 x=left+translationX y=top+translationY
 *
 * MotionEvent:
 * 触摸事件返回的封装对象：事件类型和移动距离
 *
 * TouchSlop:判断滑动事件的距离值 效应该值表示没有滑动
 *
 * 判断touch事件相关的辅助类
 *
 * 速度判断类：VelocityTracker
 * 手势判断类：GestureDetector
 * 缓慢滑动设置类：Scroller //将距离在设定的事件内分解 缓慢移动控件
 *
 *
 *
 * */
class ViewBaseActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "ViewBaseActivity ===== "
    }

    /**
     * 手势监听回调类
     *
     * */
    private val mGestureListener = object : GestureDetector.OnGestureListener {
        override fun onDown(e: MotionEvent?): Boolean {
            Log.d(TAG, "onDown() called with: e = $e")
            return false
        }

        override fun onShowPress(e: MotionEvent?) {
            Log.d(TAG, "onShowPress() called with: e = $e")
        }

        override fun onSingleTapUp(e: MotionEvent?): Boolean {
            Log.d(TAG, "onSingleTapUp() called with: e = $e")
            return false
        }

        override fun onScroll(
            e1: MotionEvent?,
            e2: MotionEvent?,
            distanceX: Float,
            distanceY: Float
        ): Boolean {
            Log.d(
                TAG,
                "onScroll() called with: e1 = $e1, e2 = $e2, distanceX = $distanceX, distanceY = $distanceY"
            )
            return false
        }

        override fun onLongPress(e: MotionEvent?) {
            Log.d(TAG, "onLongPress() called with: e = $e")
        }

        override fun onFling(
            e1: MotionEvent?,
            e2: MotionEvent?,
            velocityX: Float,
            velocityY: Float
        ): Boolean {
            Log.d(
                TAG,
                "onFling() called with: e1 = $e1, e2 = $e2, velocityX = $velocityX, velocityY = $velocityY"
            )
            return false
        }
    }

    /**
     * 双击事件监听辅助类
     *
     * */

    private val mDoubleTapListener = object : GestureDetector.OnDoubleTapListener {
        override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
            Log.d(TAG, "onSingleTapConfirmed() called with: e = $e")
            return false
        }

        override fun onDoubleTap(e: MotionEvent?): Boolean {
            Log.d(TAG, "onDoubleTap() called with: e = $e")
            return false
        }

        override fun onDoubleTapEvent(e: MotionEvent?): Boolean {
            Log.d(TAG, "onDoubleTapEvent() called with: e = $e")
            return false
        }
    }

    //或者重写SimpleOnGestureListener监听事件 监听感兴趣的事件

    private val simpleGestureListener = object : GestureDetector.SimpleOnGestureListener() {
        override fun onDoubleTap(e: MotionEvent?): Boolean {
            Log.d(TAG, "onDoubleTap() called with: e = $e")
            return super.onDoubleTap(e)
        }

        override fun onScroll(
            e1: MotionEvent?,
            e2: MotionEvent?,
            distanceX: Float,
            distanceY: Float
        ): Boolean {
            Log.d(
                TAG,
                "onScroll() called with: e1 = $e1, e2 = $e2, distanceX = $distanceX, distanceY = $distanceY"
            )
            return super.onScroll(e1, e2, distanceX, distanceY)
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_base)
//        val dd:String? = null
//        dd!!.length   查看堆栈信息 看代码调用过程
        view_base_main_button.setOnClickListener {
            val left = view_base_main_button.left
            val top = view_base_main_button.top
            val right = view_base_main_button.right
            val bottom = view_base_main_button.bottom

            val x = view_base_main_button.x
            val y = view_base_main_button.y

            val translationX = view_base_main_button.translationX
            val translationY = view_base_main_button.translationY

            Log.d(
                TAG,
                "onCreate() called left: $left right: $right top: $top bottom: $bottom x $x y $y $translationX"
            )
            Log.d(TAG, "onCreate() called x: $x y: $y")
            Log.d(TAG, "onCreate() called  translationX: $translationX translationY: $translationY")
        }

        view_base_main_button.setOnTouchListener { v, event ->
            Log.d(
                TAG,
                "onCreate: touch event action ${event.action} x: ${event.x} " +
                        "y: ${event.y} getRawX: ${event.getRawX()} getRawY: ${event.getRawY()}"
            )
            false
        }
        //移动最小距离判断值
        val scaledTouchSlop = ViewConfiguration.get(this).scaledTouchSlop
        Log.d(TAG, "onCreate: 最小移动距离判断值 $scaledTouchSlop ")

        //view的滑动速度监听辅助类
        velocity.setOnTouchListener { v, event ->
            val obtain = VelocityTracker.obtain()
            obtain.addMovement(event)
            obtain.computeCurrentVelocity(1000)
            val xVelocity = obtain.xVelocity
            val yVelocity = obtain.yVelocity
            Log.d(TAG, "onCreate: VelocityTracker X space: $xVelocity Y space: $yVelocity")
            obtain.clear()
            obtain.recycle()
            false
        }

        gesture.setOnTouchListener { v, event ->
            //如果是监听滑动相关事件 建议自己实现逻辑 如果是监听双击事件 则可以使用GestureDetector 和 OnDoubleTapListener
            val gestureDetector = GestureDetector(this, mGestureListener)
            gestureDetector.setIsLongpressEnabled(false)//防止长按屏幕无法拖动
            gestureDetector.setOnDoubleTapListener(mDoubleTapListener)
            gestureDetector.onTouchEvent(event)
            false
        }

        //缓慢滑动实现的辅助类Scroller
        scroller.setOnClickListener {
            //支持控件的缓慢滑动 并且查看view位置属性相关的值
            ViewUtil.checkViewLocation(ex_button)
            ex_button.smoothScrollTo(ex_button.scrollX + 50, ex_button.scrollY + 50)
            ViewUtil.checkViewLocation(ex_button)
        }
    }

}